package com.cwj.algorithm.priority;

import java.util.Arrays;

/**
 * @author chenwujie
 * @date 2020-12-04 11:06
 */
public class IndexPriorityQueue<E extends Comparable<? super E>> {

    private final transient Object[] items;
    private final transient int[] pq;
    private final transient int[] qp;
    private int n;
    private final boolean isMin;

    public IndexPriorityQueue(int capacity) {
        this(capacity, false);
    }

    public IndexPriorityQueue(int capacity, boolean isMin) {
        this.isMin = isMin;
        this.items = new Object[capacity];
        this.pq = new int[capacity + 1];
        this.qp = new int[capacity + 1];

        Arrays.fill(pq, -1);
        Arrays.fill(qp, -1);
    }

    public void insert(E e) {
        insert(n, e);
    }

    public boolean insert(int i, E e) {
        if (contains(i)) {
            return false;
        }
        n++;
        items[i] = e;
        pq[n] = i;
        qp[i] = n;
        swim(n);
        return true;
    }

    public boolean change(int i, E e) {
        if (i >= n || i < 0 || qp[i] == -1) {
            return false;
        }
        items[i] = e;
        int k = qp[i];
        swim(k);
        sink(k);
        return true;
    }

    public int removeFirst() {
        if (isEmpty()) {
            return -1;
        }
        int index = pq[1];
        exchange(1, n);
        qp[pq[n]] = -1;
        int k = pq[n];
        pq[n] = -1;
        items[k] = null;

        --n;
        sink(1);
        return index;
    }

    public E removeItem(int i) {
        E item = index(i);
        int k = qp[i];
        exchange(k, n);
        qp[pq[n]] = -1;
        pq[n] = -1;
        items[i] = null;
        --n;
        sink(k);
        swim(k);
        return item;

    }

    private void swim(int k) {
        if (k > n){
            return;
        }
        if (isMin) {
            while (k > 1) {
                if (greater(k / 2, k)) {
                    exchange(k, k / 2);
                }
                k = k / 2;
            }
        } else {
            while (k > 1) {
                if (greater(k, k / 2)) {
                    exchange(k, k / 2);
                }
                k = k / 2;
            }
        }
    }

    private void sink(int k) {
        sink(k, n);
    }

    private void sink(int k, int range) {
        if (isMin) {
            while (k * 2 <= range) {
                int minIndex = k * 2;
                if (2 * k + 1 <= range && greater(2 * k, k * 2 + 1)) {
                    minIndex += 1;
                }
                if (minIndex <= range && greater(k, minIndex)) {
                    exchange(minIndex, k);
                    k = minIndex;
                } else {
                    break;
                }
            }
        } else {
            while (k * 2 <= range) {
                int maxIndex = k * 2;
                if (2 * k + 1 <= range && greater(2 * k + 1, k * 2)) {
                    maxIndex += 1;
                }
                if (maxIndex <= range && greater(maxIndex, k)) {
                    exchange(maxIndex, k);
                    k = maxIndex;
                } else {
                    break;
                }
            }
        }
    }

    private boolean greater(int i, int j) {
        return index(pq[i]).compareTo(index(pq[j])) > 0;
    }

    private void exchange(int i, int j) {
        int temp = pq[i];
        pq[i] = pq[j];
        pq[j] = temp;

        qp[pq[j]] = j;
        qp[pq[i]] = i;
    }

    public boolean contains(int i) {
        return qp[i] != -1;
    }


    @SuppressWarnings("unchecked")
    public E index(int index) {
        if (isEmpty()) {
            return null;
        }
        return (E) items[index];
    }

    public int size() {
        return n;
    }

    public boolean isEmpty() {
        return this.n == 0;
    }

}
